package com.asset.dispoal.file.replyDetail.domain.service.command;

import com.asset.dispoal.file.replyDetail.domain.db.mapper.FileReplyDetailMapper;
import com.asset.dispoal.file.replyDetail.domain.dto.ReplyMonthStatistics;
import com.asset.dispoal.file.replyDetail.dto.FileReplyTypeData;
import com.asset.dispoal.file.replyDetail.dto.FileReplyYearDetailStatistics;
import com.asset.dispoal.file.replyDetail.dto.FileReplyYearMonthDetailStatistics;
import com.asset.dispoal.file.replyDetail.dto.FileReplyYearTypeStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.core.spring.util.DataUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class FileReplyDetailYearStatisticsCommand implements BaseCommand<Integer, Result<FileReplyYearDetailStatistics>> {

    @Autowired
    private FileReplyDetailMapper fileReplyDetailMapper;

    @Override
    public Result<FileReplyYearDetailStatistics> execute(Integer year) {
        FileReplyYearDetailStatistics yearDetailStatistics = new FileReplyYearDetailStatistics();
        yearDetailStatistics.setYearAmount(fileReplyDetailMapper.getYearAmount(year));
        FileReplyYearTypeStatistics monthChannelStatistics = new FileReplyYearTypeStatistics();
        FileReplyYearTypeStatistics monthBatchStatistics = new FileReplyYearTypeStatistics();

        monthChannelStatistics.setNameList(fileReplyDetailMapper.queryChannelNameByYear(year));
        monthBatchStatistics.setNameList(fileReplyDetailMapper.queryBatchNameByYear(year));

        List<ReplyMonthStatistics> batchMonthStatistics = fileReplyDetailMapper.queryMonthStatisticsByYear(year);
        List<ReplyMonthStatistics> channelMonthStatistics = fileReplyDetailMapper.queryChannelStatisticsByYear(year);

        generatorStatistics(monthBatchStatistics, batchMonthStatistics);
        generatorStatistics(monthChannelStatistics, channelMonthStatistics);

        yearDetailStatistics.setMonthChannelStatistics(monthChannelStatistics);
        yearDetailStatistics.setMonthBatchStatistics(monthBatchStatistics);
        return Result.ok(yearDetailStatistics);
    }

    private void generatorStatistics(FileReplyYearTypeStatistics monthChannelStatistics, List<ReplyMonthStatistics> channelMonthStatistics) {
        if (CollectionUtil.isNotEmpty(channelMonthStatistics)) {
            Map<Integer, FileReplyYearMonthDetailStatistics> map = new HashMap<>();
            for (ReplyMonthStatistics replyMonthStatistics : channelMonthStatistics) {
                FileReplyYearMonthDetailStatistics current = map.getOrDefault(replyMonthStatistics.getMonth(), new FileReplyYearMonthDetailStatistics());
                if (current.getAmount() == null) {
                    current.setAmount(BigDecimal.ZERO);
                    current.setMonth(replyMonthStatistics.getMonth());
                    current.setDataList(new ArrayList<>());
                }
                current.setAmount(current.getAmount().add(replyMonthStatistics.getAmount()));
                current.getDataList().add(DataUtil.copyTo(replyMonthStatistics, FileReplyTypeData.class));
                map.put(replyMonthStatistics.getMonth(), current);
            }
            monthChannelStatistics.setDataList(new ArrayList<>(map.values()));
        }
    }
}
